<?php
function createHtml($url,$dir,$filename){

    $content = file_get_contents($url);                 //获取内容

    if (!is_dir($dir)){                                 //如果目录不存在
        mkdir(iconv("UTF-8", "GBK", $dir),0777,true);   //创建目录(777权限)
    }

    //-------------------------------------------处理导航链接-------------------------
    $listurls = "/[\'|\"]\/App\/Index\/articalList\/id\/(.*?)[\'|\"]/";
    preg_match_all($listurls,$content,$matchlist);
    foreach ( $matchlist[1] as $key => $value ) {
        $value = trim($value);
        $map['catdir'] = $value;
        $cat = M('cms_cate')->where($map)->limit(1)->find();
        if($cat['lv'] == 1){
            $listurl = "\"/HTML/".$value.".html\"";
        }else if(!empty($cat['pid']) && ($cat['lv'] == 2)){
            $pid = $cat['pid'];
            $tmap['id']=$pid;
            $pcat = M('cms_cate')->where($tmap)->limit(1)->find();
            $pcatdir = $pcat['catdir'];
            $listurl = "\"/HTML/".$pcatdir."/".$value.".html\"";
        }

        $content = str_replace ( $matchlist[0][$key], $listurl, $content );
    }
    //-----------------------------------------------------------------------------------


    //------------------------------------处理文章列表页---------------------------------
    //列表页链接进入内容页
    $infourl = "/href=[\'|\"]\/App\/Index\/articalInfo\/id\/(.*?)[\'|\"]/";
    preg_match_all($infourl,$content,$matchlist);
    foreach($matchlist[1] as $key => $value){
        $value = trim($value);
        $map['id'] = $value;
        $cateid = M('Artical')->where($map)->limit(1)->getField('cateid');
        $cat = M('cms_cate')->where('id=' . $cateid)->limit(1)->find();
        if($cat['lv'] == 1){
            $info = "href='/HTML/".$cat['catdir']."/".$value.".html'";
        }else if(!empty($cat['pid']) && ($cat['lv'] == 2)){
            $pid = $cat['pid'];
            $tmap['id']=$pid;
            $pcat = M('cms_cate')->where($tmap)->limit(1)->find();
            $pcatdir = $pcat['catdir'];
            $info = "href='/HTML/".$pcatdir."/".$cat['catdir']."/".$value.".html'";
        }

        $content = str_replace ( $matchlist[0][$key], $info, $content );
    }

    //文章列表分页
    $page = "/<a (.*?) href=[\'|\"]\/Index\/articalList\/id\/(.*?)\/p\/(.*?)[\'|\"]>/";
    preg_match_all($page,$content,$matchpage);
    foreach ( $matchpage[3] as $key1 => $value1 ) {
        foreach ( $matchpage[1] as $key2 => $value2 ) {
            if($key1 == $key2){
                $value1 = trim($value1);
                $value2 = trim($value2);
                $url = '<a '.$value2.' href="/HTML/'.$matchpage[2][0].'_'.$value1.'.html">';
                $content = str_replace ( $matchpage[0][$key1], $url, $content );
            }
        }
    }
    //-----------------------------------------------------------------------------------

    //生成的文件路径与文件名
    $path = $dir."/".$filename.".html";
    if(file_exists($path)){
        unlink($path);                   //删除已有的同名文件
    }
    //'w' 写入方式打开，将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
    $fp = fopen($path, "w");
    $flag = fwrite($fp, $content);       //写入内容
    fclose($fp);

    return $flag;                        //返回写入标识[成功/失败]
}
